package com.gy.spring.mvc.common.interceptor;

import com.gy.spring.mvc.common.tools.R;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import org.springframework.web.servlet.NoHandlerFoundException;

import java.io.IOException;

@RestControllerAdvice
public class ExceptionCapture {
    private Log logger = LogFactory.getLog(getClass());

    @ExceptionHandler(IOException.class)
    public R handlerIOException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.err("流文件异常");
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public R handlerNoFoundException(Exception e) {
        logger.error(e.getMessage(), e);
        return R.err(404, "请求地址不存在");
    }

    @ExceptionHandler(Exception.class)
    public R handlerException(Exception e) {
        System.err.println("未知异常");
        logger.error(e.getMessage(), e);
        return R.err("未知异常");
    }

}
